# -*- coding: utf-8 -*-
# @Author：fanmingxiang@touty.io
# @Date  ：2022/5/9 15:23
from pure_pagination.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.utils.safestring import mark_safe


class Pagination:

    def __init__(self, data_list, search_text, page, nums):

        self.nums_list = [5, 10, 15, 20]
        self.nums = nums
        self.page = page
        self.data_list = data_list
        self.search_text = search_text

    def per_page(self):
        nums_list = [5, 10, 15, 20]
        if self.nums:
            page_num = int(self.nums)
        else:
            page_num = 10
        counts = self.data_list.count()
        pag = Paginator(self.data_list, page_num)
        try:
            obj_list = pag.page(self.page)
        except EmptyPage:
            obj_list = pag.page(1)
        content = {
            "obj_list": obj_list,
            "counts": counts,
            "nums_list": nums_list,
            "search_text": self.search_text
        }
        return content
